import { Controller, Get, Post, Res, UseInterceptors, UploadedFile, Body, ParseFilePipe, UploadedFiles } from '@nestjs/common';
// import { createReadStream } from 'fs';
// import { join } from 'path';
import { FileFieldsInterceptor } from '@nestjs/platform-express';
import { Public } from '../metadatas/public.metadata';

@Controller('/api/open')
export class OpenController {
  @Post()
  getFile(@Res() res: Response) {
    // console.log(1);
    // const file = createReadStream(join(process.cwd(), 'package.json'));
    // // @ts-ignore
    // file.pipe(res);
    // console.log(res);
    // console.log(1);
  }

  @Public()
  @Post('/upload')
  @UseInterceptors(
    FileFieldsInterceptor([
      { name: 'file', maxCount: 1 },
      { name: 'avatar', maxCount: 1 },
      { name: 'background', maxCount: 1 }
    ])
  )
  uploadFile(@UploadedFiles() files: Record<string, Express.Multer.File[] | undefined>) {
    const keys = Object.keys(files);
    const rows: Record<string, any>[] = keys.map((key) => {
      const [file] = files[key] || [];
      console.log(1, file);
      return {
        fieldname: file.fieldname,
        originalname: file.originalname,
        mimetype: file.mimetype,
        size: file.size
      };
    });
    return rows;
  }
}
